define(function(require, exports, module) {
	'use strict';
    
    var rk = require('rk');
    
    
    require('../su/accordion/oaTabCtrl');
    
    
    //引入jstree
    $.widget('rk.colleagueDepartPanelCtrl', $.rk.oaPanelCtrl, {
        _create: function () {
            var me = this;
            var elem = me.element;
            //1，模板文件请在WEB-INFO/templates/oa中追加
            //2，请在src/resources/sub-deps/oa中引入
            //3，如下所示，调用rk.template方法生成html
            me._listeners();
        },
        _listeners: function () {
            var me = this;
            var elem = me.element;
            var clean_depart_query_text = function(){
                elem.find("[act='colleague_depart_query_text']").val("");
                elem.find("[act='colleague_depart_query_text_clear']").hide();
            };
            elem.on('input propertychange', "[act='colleague_depart_query_text']", function(){
                var val = elem.find("[act='colleague_depart_query_text']").val();
                if(val){
                    elem.find("[act='colleague_depart_query_text_clear']").show();
                }else{
                    elem.find("[act='colleague_depart_query_text_clear']").hide();
                }
            });
            elem.on("click", "[act='colleague_depart_query_text_clear']", function(){
                clean_depart_query_text();
            });
            elem.on('click', "[act='colleague_depart_query_button']", function(){
                var departName = elem.find("[act='colleague_depart_query_text']").val();
                if(departName){
                    var param = {pageNo:1,pageSize:20};
                    param.departName = departName;
                    me.loaddata("depart",param);
                }
            });
            elem.on("keydown", "[act='colleague_depart_query_text']", function(e){
                if (e.keyCode == 13) {
                    var departName = elem.find("[act='colleague_depart_query_text']").val();
                    if(departName){
                        var param = {pageNo:1,pageSize:20};
                        param.departName = departName;
                        me.loaddata("depart",param);
                    }
                }
            });
            elem.on("click", "[act='colleague_depart_return_tree']", function(){
                me.loaddata("depart_tree");
                clean_depart_query_text();
            });
            elem.on("click", "[act='colleague_depart_query_table_row']", function(){
                var row = $(this);
                var param = {};
                param.type = "depart";
                param.gid = row.attr("gid");
                me._showDetails(param,"middle");
            })
        },
        _showSearch: function(){
            var me = this;
        },
        _showDetails: function(param,size){
            var me = this;
            me.popupContent({
                size: size,
                onSuccess: function (contentElem) {
                    require.async(["./colleagueDepartContentCtrl"], function () {
                        contentElem.colleagueDepartContentCtrl(param);
                    });
                }
            });
        },
        //init方法中，用来初始化数据
        _init: function () {
            var me = this;
            var elem = me.element;
            me.loaddata("depart_tree");
        },
        loaddata: function (type, param) {
            var me = this;
            var elem = me.element;
            if (type === "depart") {
                elem.find('[beScrollAct="beScroll"]').beScroll();
                elem.find("[act='colleague_depart_tab_body']").beLoading();
                elem.find('[act="colleague_depart_query_page_div"]').hide();
                rk.post("/json/oa_group/departments.action",param,function(json){
                    //elem.find("[act='group_depart_tree']")
                    if(json.groups && json.groups.length > 0) {
                        require.async(["oa/tmpl/colleague/tpl_colleague_depart_query.tpl"], function (temp) {
                            //elem.find("[act='coleague_depart_tab_body']").html(temp);
                            //elem.html(temp);
                            var html = rk.templateText(temp, json);
                            elem.find("[act='colleague_depart_tab_body']").html(html);
                            if (elem.find("[act='colleague_depart_header']")) {
                                elem.find("[act='colleague_depart_header']").show();
                            }
                            elem.find("[act='colleague_depart_query_count']").html(json.dataCount);
                            elem.find('[act="colleague_depart_query_page_div"]').rkPaging({
                                pagenum: json.pageNo,  //（可选）当前页数，默认1
                                pagesize: json.pageSize,  //（可选）当前每页显示条数，默认20
                                rowscount: json.dataCount, //所有数据数
                                onChange: function (pagenum) {
                                    var params = param;
                                    params.pageNo = pagenum;
                                    params.pageSize = json.pageSize;
                                    me.loaddata(type, params);
                                }
                            });
                        });
                    }else{
                        require.async(["oa/tmpl/colleague/tpl_colleague_none.tpl"], function (temp) {
                            elem.find("[act='colleague_depart_query_count']").html("0");
                            var html = rk.templateText(temp, {text: rk.i18n('COLLEAGUE_NO_SUITABLE_DEPART')});
                            elem.find("[act='colleague_depart_tab_body']").html(html);
                        });
                    }
                });
            } else if (type === "depart_tree") {
                var temp = require('oa/tmpl/colleague/tpl_colleague_depart_tree.tpl');
                var html = rk.templateText(temp,{});
                elem.html(html);
                elem.find('[beScrollAct="beScroll"]').beScroll();
                elem.find("[act='group_depart_tree']").beLoading();
                rk.post("/json/oa_group/depart-tree.action?dimType=1",{},function(json){
                    elem.find("[act='group_depart_tree']").jstree({
                        'core': {
                            data:json.data.tree,
                            "types": {
                                "default": {
                                    draggable: false
                                }
                            },
                            'themes': {
                                name: 'default-white',
                                url: true,
                                dir: seajs.data.base + 'lib/jstree/3.0.9/themes/',
                                dots: false,
                                icons: false,
                                stripes: false,
                                variant: false,
                                responsive: false
                            }
                        },
                        'plugins': ['themes', 'types']
                    }).bind('select_node.jstree', function(e,data){
                        var param = {};
                        var node = data.node;
                        if(node.original){
                            param.gid = node.original.attr.groupId;
                        }
                        param.type = "depart";
                        me._showDetails(param,"middle");
                    }).bind("loaded.jstree",function(e,data){
                        var maxNodes = data.instance._model.data;
                        for(var key in maxNodes){
                            if(!maxNodes[key].parent || maxNodes[key].parent == "#") {
                                data.instance.open_node(maxNodes[key]);
                            }
                        }
                    });
                });
            }
        }
    });
});